package com.maimang.platform.config;

import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.xss.XssFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname FilterConfig
 * @description XSS Filter配置
 * @date 2019-04-23 19:45
 * @email jintaozhao@qq.com
 */
@Configuration
public class FilterConfig {

	@Value("${xss.enabled}")
	private String enabled;

	@Value("${xss.excludes}")
	private String excludes;

	@Value("${xss.urlPatterns}")
	private String urlPatterns;

	@Bean
	public FilterRegistrationBean xssFilterRegistration() {
		FilterRegistrationBean xssRegistration = new FilterRegistrationBean();
		xssRegistration.setDispatcherTypes(DispatcherType.REQUEST);
		xssRegistration.setFilter(new XssFilter());
		xssRegistration.addUrlPatterns(StringUtils.split(urlPatterns, ","));
		xssRegistration.setName("xssFilter");
		xssRegistration.setOrder(Integer.MAX_VALUE);
		Map<String, String> initParameters = new HashMap<>();
		initParameters.put("enabled", enabled);
		initParameters.put("excludes", excludes);
		xssRegistration.setInitParameters(initParameters);
		return xssRegistration;
	}
}
